package cn.elwy.osgi.config;

import java.io.File;

import cn.elwy.common.config.Config;
import cn.elwy.osgi.config.model.Session;
import cn.elwy.osgi.config.model.WindowConfig;
import cn.elwy.osgi.config.util.ThemeUtil;

public class ConfigContext {

	public static final String DEFAULT_ENCODING = Constant.ENCODING_UTF8;

	public static final String PROJECT_EXT_NAME = "project";
	// private static final String APP_HOME_DIR = "/data/";
	// public static final String USER_DIR = "user/";
	// public static final String DATA_APP_DIR = APP_HOME_DIR + "app/";
	// public static final String DATA_USER_DIR = APP_HOME_DIR + "user/";
	public static final String DAO_PREFIX = "dao";
	public static final String SERVICE_PREFIX = "service";

	private static final Session session = new Session();
	private static final WindowConfig windowConfig = new WindowConfig();

	// private static File dataHome;
	private static File javaHome;
	private static File mavenHome;

	private static String themeDir;
	private static String themeName;
	private static String subjectType;
	private static String dialogConfigDir;
	private static String tableConfigDir;
	private static String saveStrategy;

	private static boolean isGetServiceByName;
	private static boolean isGetDaoByName;
	private static String daoPrefix;
	private static String servicePrefix;
	private static String auditPrefix;

	private static Config config;
	private static boolean usedLastNewProject;

	static {
		// dataHome = new File(ResourceUtil.INSTALL_PATH, APP_HOME_DIR);
		AppConfig appConfig = AppConfig.getInstance();
		appConfig.load();
		javaHome = appConfig.getJavaHome();
		mavenHome = appConfig.getMavenHome();
		// logHome = sysConfig.getLogHome();

		appConfig.initWindowConfig(windowConfig);

		themeDir = appConfig.getThemeDir();
		themeName = appConfig.getThemeName();
		dialogConfigDir = appConfig.getDialogConfigDir();
		tableConfigDir = appConfig.getTableConfigDir();
		subjectType = appConfig.getSubjectType();
		saveStrategy = appConfig.getSaveStrategy();

		isGetDaoByName = appConfig.isGetDaoByName();
		isGetServiceByName = appConfig.isGetServiceByName();
		daoPrefix = appConfig.getDaoPrefix();
		servicePrefix = appConfig.getServicePrefix();
		auditPrefix = appConfig.getAuditPrefix();
		usedLastNewProject = appConfig.getUsedLastNewProject();
	}

	public static void init() {
		initSysConfig();
		initConfig();
    ThemeUtil.init();
	}

	public static void reset() {
		AppConfig.dispose();
		init();
	}

	public static void initSysConfig() {
	}

	public static void initConfig() {
		config = new Config(ConfigContext.class);
		config.load(Constant.CONFIG_FILE);
		session.setUserId(config.get("user.id", "1"));
		session.setRoleId(config.get("role.id", "1"));
		session.setOrgId(config.get("org.id", "1"));
	}

	public static Session getSession() {
		return session;
	}

	public static WindowConfig getWindowconfig() {
		return windowConfig;
	}

	// public static File getDataHome() {
	// return dataHome;
	// }

	// public static File getAppHome() {
	// return appHome;
	// }
	//
	// public static File getUserHome() {
	// return userHome;
	// }
	//
	// public static File getLogHome() {
	// return logHome;
	// }

	public static File getJavaHome() {
		return javaHome;
	}

	public static File getMavenHome() {
		return mavenHome;
	}

	public static String getThemeDir() {
		return themeDir;
	}

	public static String getThemeName() {
		return themeName;
	}

	public static String getSubjectType() {
		return subjectType;
	}

	public static String getDialogConfigDir() {
		return dialogConfigDir;
	}

	public static String getTableConfigDir() {
		return tableConfigDir;
	}

	public static String getSaveStrategy() {
		return saveStrategy;
	}

	public static boolean isGetServiceByName() {
		return isGetServiceByName;
	}

	public static boolean isGetDaoByName() {
		return isGetDaoByName;
	}

	public static String getDaoPrefix() {
		return daoPrefix;
	}

	public static String getServicePrefix() {
		return servicePrefix;
	}

	public static String getAuditPrefix() {
		return auditPrefix;
	}

	public static Config getConfig() {
		return config;
	}

	public static boolean isUsedLastNewProject() {
		return usedLastNewProject;
	}

}
